package com.zqx.dryproxy.handler;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.Calendar;
import java.util.GregorianCalendar;

/**
 * description: 自定义请求处理类
 * @author zqx
 * @date 2018/09/04
 */
public class DaoLogHandler implements InvocationHandler {

    private Calendar calendar;
    private Object object;

    public DaoLogHandler() {
    }
    //自定义有参构造函数，用于注入一个需要提供代理的真实主题对象
    public DaoLogHandler(Object object) {
        this.object = object;
    }

    //实现invoke()方法，调用在真实主题类中定义的方法
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        beforeInvoke();
        Object result = method.invoke(object, args);
        afterInvoke();
        return result;
    }

    public void beforeInvoke(){
       calendar = new GregorianCalendar();
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);
        int second = calendar.get(Calendar.SECOND);
        String time = hour+":"+minute+":"+second;
        System.out.println("调用开始时间："+time);
        long start = System.nanoTime();
        System.out.println("调用开始时间："+start);
    }

    public void afterInvoke(){
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);
        int second = calendar.get(Calendar.SECOND);
        String time = hour+":"+minute+":"+second;
        System.out.println("调用结束时间："+time);
        //使用System.nanoTime()是为了显示开始和结束时间不一致
        long end = System.nanoTime();
        System.out.println("调用结束时间："+end);
        System.out.println("方法调用结束");
    }
}
